package com.mww.chatroom.controller;

import com.mww.chatroom.mapper.MessageMapper;
import com.mww.chatroom.mapper.MessageSessionMapper;
import com.mww.chatroom.model.Friend;
import com.mww.chatroom.model.MessageSession;
import com.mww.chatroom.model.MessageSessionUserItem;
import com.mww.chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MessageSessionController {
    @Resource
    private MessageSessionMapper messageSessionMapper;
    @Resource
    private MessageMapper messageMapper;

    @GetMapping("/sessionList")
    public Object getMessageSessionList(HttpServletRequest req){
        List<MessageSession> messageSessionList = new ArrayList<>();
        //1.获取到当前用户的userId(从spring中的session中获取)
        HttpSession session = req.getSession();
        if(session==null){
            System.out.println("[getMessageSessionList] session==null");
            return  messageSessionList;
        }

        User user = (User) session.getAttribute("user");
        if(user==null){
            System.out.println("[getMessageSessionList] user==null");
            return messageSessionList;
        }
        //2.根据userId查询数据库，查出来有哪些会话id
        List<Integer> sessionIdList = messageSessionMapper.getSessionIdsByUserId(user.getUserId());
        for(int sessionId : sessionIdList){
            MessageSession messageSession = new MessageSession();
          List<Friend> friends = messageSessionMapper.getFriendsBySessionId(sessionId,user.getUserId());
            //3.遍历会话id,查询出每个会话涉及到的好友都有谁
          messageSession.setSessionId(sessionId);
          messageSession.setFriends(friends);
//4.遍历会话id,查询出每一个会话的最后一条消息//TODO
            //可能出现按照会话id查不到的情况
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage==null){
                lastMessage="";
            }else {
                messageSession.setLastMessage(lastMessage);
            }

            messageSessionList.add(messageSession);
        }


        //最终构造成messagesession对象
return messageSessionList;

    }
    @Transactional
    @PostMapping("/session")
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user){
        HashMap<String,Integer> resp = new HashMap<>();
//进行数据库的插入操作
        //1.先给messageSession插入对象，使用这个参数主要是能够获取会话的主键sessionId
    MessageSession messageSession = new MessageSession();
   messageSessionMapper.addMessageSession(messageSession);
   //给message_session_user表插入记录
        MessageSessionUserItem item1 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setSessionId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);
       //给message_session_user表插入记录
        MessageSessionUserItem item2 = new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setSessionId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);
        System.out.println("[addMessageSession]新增会话成功！ sessionId= " +messageSession.getSessionId()
        +",userId1="+user.getUserId()+",userId2="+toUserId);

resp.put("sessionId",messageSession.getSessionId());
return resp;

    }
}
